package com.cxb.java.face;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Classname Test
 * @Date 2023/9/10 11:32
 * @Created by Administrator
 * @Description TODO
 */
public class Test1 {

    public static void main(String[] args) throws Exception {
        // 方法1：获取class
        Class<?> aClass = Class.forName("com.cxb.java.face.MyUser");
        System.out.println(aClass);
        // 方法1：创造对象
        MyUser o = (MyUser) aClass.newInstance();
        System.out.println(o);

        // 方法2：获取class
        Class<?> bClass = MyUser.class;
        System.out.println(bClass);
        // 方法2：创造对象
        Constructor<?> con = bClass.getConstructor();
        MyUser o1 = (MyUser) con.newInstance();
        System.out.println(o1);

        // 方法3：获取class
        Class<?> cClass = new MyUser().getClass();
        System.out.println(cClass);
        System.out.println("******************");
        // 利用反射获取构造方法
        Class<?> dClass = Class.forName("com.cxb.java.face.MyUser");
        Constructor<?>[] declaredConstructors = dClass.getDeclaredConstructors();
        for (Constructor c : declaredConstructors) {
            System.out.println(c);
        }
        // 获取所有的public的方法
        Method[] methods = dClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("********getDeclaredMethods**********");
        // 获取类的所有方法
        Method[] declaredMethods = dClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method);
        }
        System.out.println("**********getDeclaredFields********");
        Field[] declaredFields = dClass.getDeclaredFields();
        for (Field field: declaredFields) {
            System.out.println(field);
        }
        System.out.println("********getFields**********");
        Field[] fields = dClass.getFields();
        for (Field field: fields) {
            System.out.println(field);
        }


    }
}
